package com.huacai.collect.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.huacai.common.annotation.Log;
import com.huacai.common.core.controller.BaseController;
import com.huacai.common.core.domain.AjaxResult;
import com.huacai.common.enums.BusinessType;
import java.io.InputStream;
import org.springframework.web.multipart.MultipartFile;
import com.huacai.collect.domain.Collect;
import com.huacai.collect.service.ICollectService;
import com.huacai.common.utils.poi.ExcelUtil;
import com.huacai.common.core.page.TableDataInfo;

/**
 * 博客收藏Controller
 *
 * @author lundao
 * @date 2025-09-14
 */
@RestController
@RequestMapping("/huacai/collect")
public class CollectController extends BaseController
{
    @Autowired
    private ICollectService collectService;

    /**
     * 查询博客收藏列表
     */

    @GetMapping("/list")
    public TableDataInfo list(Collect collect)
    {
        startPage();
        List<Collect> list = collectService.selectCollectList(collect);
        return getDataTable(list);
    }
    /**
     * 根据userId和blogId删除博客收藏
     */
    @DeleteMapping("/deleteCollectByUserIdAndBlogId/{blogId}")
    public AjaxResult deleteCollectByUserIdAndBlogId(@PathVariable String blogId) {
        return toAjax(collectService.deleteCollectByUserIdAndBlogId(blogId));
    }
    /**
     * 导出博客收藏列表
     */

    @Log(title = "博客收藏", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Collect collect)
    {
        List<Collect> list = collectService.selectCollectList(collect);
        ExcelUtil<Collect> util = new ExcelUtil<Collect>(Collect.class);
        util.exportExcel(response, list, "博客收藏数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Collect> util = new ExcelUtil<Collect>(Collect.class);
        util.importTemplateExcel(response, "博客收藏数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "博客收藏", businessType = BusinessType.IMPORT)

    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<Collect> util = new ExcelUtil<Collect>(Collect.class);
        InputStream inputStream = file.getInputStream();
        List<Collect> list = util.importExcel(inputStream );
        inputStream.close();
        int count = collectService.batchInsertCollect(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取博客收藏详细信息
     */

    @GetMapping(value = "/{collectId}")
    public AjaxResult getInfo(@PathVariable("collectId") String collectId)
    {
        return success(collectService.selectCollectByCollectId(collectId));
    }

    /**
     * 新增博客收藏
     */

    @Log(title = "博客收藏", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Collect collect)
    {
        return toAjax(collectService.insertCollect(collect));
    }

    /**
     * 修改博客收藏
     */

    @Log(title = "博客收藏", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Collect collect)
    {
        return toAjax(collectService.updateCollect(collect));
    }

    /**
     * 删除博客收藏
     */

    @Log(title = "博客收藏", businessType = BusinessType.DELETE)
	@DeleteMapping("/{collectIds}")
    public AjaxResult remove(@PathVariable String[] collectIds)
    {
        return toAjax(collectService.deleteCollectByCollectIds(collectIds));
    }
}
